package com.pijunn.report

import com.pijunn.common.MailingAddress;
import com.pijunn.common.enums.ServiceType;

import grails.validation.Validateable;

	
@Validateable
class CollectRewardCommand {
	String rewardAmount
	Integer eventId
	String previous
	String wizardStep = "reward"
	Boolean ship
	Boolean shipDefault = true

	FoundEvent event
	
	Contact contact = new Contact()
	MailingAddress mailing = new MailingAddress()
	String phone
	Boolean useSms
	String carrier
	ServiceType speed
	String packageType
	Float estimatedWeight
	String insuredAmount
	BigDecimal shippingAmount
	
	BigDecimal chargeamount
	String stripeToken
	
	BigDecimal getReward() {
		def convert
		
		if (rewardAmount.indexOf('$') > -1)
			convert = new BigDecimal(rewardAmount.substring(rewardAmount.indexOf('$')+1))
		else
			convert = new BigDecimal(rewardAmount)
		
		convert
	}
	
	static constraints = {
		rewardAmount (nullable: true, validator:{val, obj -> 
			if (obj.wizardStep == 'reward' && !val) 
				return 'found.rewardAmount.blank.message'
		})
		eventId (blank:false)
		wizardStep (blank: false)
		contact (nullable: true, validator:{val, obj ->
			if (obj.wizardStep == 'shipping' && obj.ship == true && !obj.previous) {
				if (!val.name)
					return 'found.contactName.blank.message'
				if (!val.email)
					return 'found.email.blank.message'
				if (val.email && !val.email.matches(/^(?i)[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/))
					return 'found.contactEMail.matches.invalid'
			}
		})
		phone (nullable: true, validator:{val, obj ->
			if (obj.wizardStep == 'shipping' && !obj.previous && !val)
				return 'found.contactPhone.blank.message'
			if (obj.wizardStep == 'contact' && !obj.previous && !val.matches(/^[+]?([0-9]*[\.\s\-\(\)]|[0-9]+){3,24}$/))
				return 'found.contactPhone.matches.invalid'
		})
		carrier (nullable:true, validator:{val, obj ->
			if (obj.wizardStep == 'shipping' && !obj.previous && !val) 
				return 'collectReward.carrier.blank.message'
		})
		speed (nullable:true, validator:{val, obj ->
			if (obj.wizardStep == 'shipping' && !obj.previous && !val)
				return 'collectReward.speed.blank.message'
		})
		packageType (nullable:true, validator:{val, obj ->
			if (obj.wizardStep == 'shipping' && !obj.previous && !val)
				return 'collectReward.packageType.blank.message'
		})
		estimatedWeight (nullable:true, validator:{val, obj ->
			if (obj.wizardStep == 'shipping' && !obj.previous && !val)
			return 'collectReward.estimatedWeight.blank.message'
		})
		insuredAmount (nullable:true, validator:{val, obj ->
			if (obj.wizardStep == 'shipping' && !obj.previous && val && !val.matches(/^[0-9.]*$/))
			return 'collectReward.insuredAmount.matches.invalid'
		})
	}
}
